package main

func restoreMatrix(rowSum []int, colSum []int) [][]int {
	grid := make([][]int, len(rowSum))
	for i := range grid {
		grid[i] = make([]int, len(colSum))
	}

	row := 0
	col := 0
	for row < len(rowSum) && col < len(colSum) {
		if rowSum[row] > colSum[col] {
			rowSum[row] -= colSum[col]
			grid[row][col] = colSum[col]
			col++
		} else {
			colSum[col] -= rowSum[row]
			grid[row][col] = rowSum[row]
			row++
		}
	}

	return grid
}
